using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using XFGameFramework;
using XFABManager;
using System;

public class SceneController : Controller
{
    public void LoadScene(string sceneName, Action onFinish, Action<float> progress)
    {
        if (SceneManager.GetSceneByName(sceneName).IsValid())
        {
            //onFinish?.Invoke();
            progress?.Invoke(1f);
            return;
        }

        CoroutineStarter.Start(LoadSceneExcute(sceneName, onFinish, progress));
    }

    public IEnumerator LoadSceneExcute(string sceneName, Action onFinish, Action<float> progress)
    {
        AsyncOperation asyncOperation = AssetBundleManager.LoadSceneAsync(Module.ProjectName,sceneName,LoadSceneMode.Single);
        while (!asyncOperation.isDone)
        {
            progress?.Invoke(asyncOperation.progress);
            yield return null;
        }

        onFinish?.Invoke();
        progress?.Invoke(1f);
    }
}
